<script setup lang="ts">
import { ref, watch } from 'vue';
import { Layout, Button, Drawer, Tooltip, ConfigProvider, theme } from 'ant-design-vue';
import { useRoute } from 'vue-router';
import GlobalHeader from './GlobalHeader.vue';
import GlobalFooter from './GlobalFooter.vue';
import {
  MenuFoldOutlined,
  MenuUnfoldOutlined,
  BulbOutlined,
  BulbFilled
} from '@ant-design/icons-vue';

const { Content, Sider } = Layout;
const { defaultAlgorithm, darkAlgorithm } = theme;

// 侧边栏折叠控制
const collapsed = ref(false);
const toggleCollapsed = () => {
  collapsed.value = !collapsed.value;
};

// 移动端抽屉控制
const drawerVisible = ref(false);
const showDrawer = () => {
  drawerVisible.value = true;
};
const closeDrawer = () => {
  drawerVisible.value = false;
};

// 暗黑模式切换
const isDarkMode = ref(false);
const toggleDarkMode = () => {
  isDarkMode.value = !isDarkMode.value;
  document.body.classList.toggle('dark-theme', isDarkMode.value);
  localStorage.setItem('darkMode', String(isDarkMode.value));
};

// 从本地存储加载暗黑模式设置
const initDarkMode = () => {
  const savedDarkMode = localStorage.getItem('darkMode');
  if (savedDarkMode === 'true') {
    isDarkMode.value = true;
    document.body.classList.add('dark-theme');
  }
};

// 监听路由变化关闭抽屉
const route = useRoute();
watch(() => route.path, () => {
  drawerVisible.value = false;
});

// 初始化暗黑模式
initDarkMode();
</script>

<template>
  <ConfigProvider :theme="{
    algorithm: isDarkMode ? darkAlgorithm : defaultAlgorithm,
    token: {
      colorPrimary: '#1668dc',
      borderRadius: 8
    }
  }">
    <Layout class="main-layout">
      <!-- 移动端顶部导航 -->
      <GlobalHeader @menu-click="showDrawer" :is-mobile="true" class="mobile-header" />

      <!-- PC端侧边栏 -->
      <Sider
        v-model:collapsed="collapsed"
        collapsible
        :trigger="null"
        theme="light"
        class="sidebar"
        :width="256"
      >
        <div class="logo-container">
          <div class="logo-content">
            <img src="@/assets/marketplace-aigc.svg" class="logo-img" />
            <span class="logo-text" v-if="!collapsed">AI Generator</span>
          </div>
        </div>

        <div class="sidebar-menu">
          <GlobalHeader :is-mobile="false" :is-collapsed="collapsed" />
        </div>
      </Sider>

      <Layout class="site-layout">
        <div class="header-toolbar">
          <Button
            type="text"
            class="trigger-btn"
            @click="toggleCollapsed"
          >
            <MenuUnfoldOutlined v-if="collapsed" />
            <MenuFoldOutlined v-else />
          </Button>
          <div class="toolbar-actions">
            <Tooltip :title="isDarkMode ? '切换至亮色模式' : '切换至暗色模式'">
              <Button type="text" @click="toggleDarkMode" class="action-button">
                <BulbFilled v-if="isDarkMode" />
                <BulbOutlined v-else />
              </Button>
            </Tooltip>
            <div class="user-avatar">
              <img src="https://api.dicebear.com/7.x/avataaars/svg?seed=user123" alt="User Avatar" />
            </div>
          </div>
        </div>

        <div class="main-content-wrapper">
          <Content class="content">
            <div class="content-container">
              <RouterView v-slot="{ Component }">
                <transition name="fade" mode="out-in">
                  <component :is="Component" />
                </transition>
              </RouterView>
            </div>
            <div class="content-spacer"></div>
          </Content>
        </div>

        <GlobalFooter class="global-footer" />
      </Layout>

      <!-- 移动端侧边抽屉 -->
      <Drawer
        placement="left"
        :open="drawerVisible"
        @close="closeDrawer"
        width="256"
        closable
        :bodyStyle="{ padding: 0 }"
      >
        <div class="drawer-logo">
          <img src="/favicon.ico" class="logo-img" />
          <span class="logo-text">AI Generator</span>
        </div>
        <div class="drawer-menu">
          <GlobalHeader :is-mobile="true" @menu-click="closeDrawer" />
        </div>
      </Drawer>
    </Layout>
  </ConfigProvider>
</template>

<style>
/* 全局暗黑模式样式 */
body.dark-theme {
  background-color: #121212;
  color: rgba(255, 255, 255, 0.85);
}

/* 过渡动画 */
.fade-enter-active,
.fade-leave-active {
  transition: opacity 0.3s ease;
}

.fade-enter-from,
.fade-leave-to {
  opacity: 0;
}
</style>

<style scoped>
.main-layout {
  min-height: 100vh;
  display: flex;
}

.logo-container {
  height: 64px;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0 24px;
  overflow: hidden;
  border-bottom: 1px solid rgba(0, 0, 0, 0.06);
}

.logo-content {
  display: flex;
  align-items: center;
  gap: 10px;
}

.logo-img {
  height: 28px;
  width: auto;
  animation: pulse 3s infinite;
}

.logo-text {
  font-size: 18px;
  font-weight: 600;
  background-image: linear-gradient(45deg, #1668dc, #13c2c2);
  background-clip: text;
  -webkit-background-clip: text;
  color: transparent;
  white-space: nowrap;
}

.site-layout {
  transition: all 0.2s;
  flex: 1;
  display: flex;
  flex-direction: column;
  margin-left: 256px; /* 添加左边距，等于侧边栏宽度 */
  padding-bottom: 0; /* 移除底部内边距 */
  position: relative; /* 添加相对定位 */
}

/* 侧边栏折叠时调整内容区域边距 */
.sidebar.ant-layout-sider-collapsed + .site-layout {
  margin-left: 80px; /* 侧边栏折叠时的宽度 */
}

.sidebar {
  height: 100vh;
  position: fixed;
  left: 0;
  top: 0;
  bottom: 0;
  z-index: 10;
  box-shadow: 2px 0 8px rgba(0, 0, 0, 0.06);
  transition: all 0.2s;
}

.sidebar-menu {
  height: calc(100% - 64px);
  overflow-y: auto;
  overflow-x: hidden;
}

.header-toolbar {
  height: 64px;
  padding: 0 24px;
  background: white;
  display: flex;
  align-items: center;
  justify-content: space-between;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.08);
  position: sticky;
  top: 0;
  z-index: 5;
}

.trigger-btn {
  font-size: 18px;
}

.main-content-wrapper {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: calc(100vh - 130px); /* 设置最小高度，减去头部和底部的高度 */
  overflow: auto; /* 允许内容滚动 */
}

.content {
  margin: 24px;
  margin-bottom: 24px;
  padding: 24px;
  background: white;
  border-radius: 8px;
  min-height: 280px;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.06);
  transition: all 0.3s ease;
  display: flex;
  flex-direction: column;
  flex: 1;
}

.content-container {
  flex: 1;
}

.content-spacer {
  height: 80px; /* 进一步增加底部间隔 */
}

.toolbar-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

.action-button {
  font-size: 18px;
}

.user-avatar {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  overflow: hidden;
  cursor: pointer;
  transition: transform 0.3s;
}

.user-avatar:hover {
  transform: scale(1.1);
}

.user-avatar img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.drawer-logo {
  height: 64px;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 0 24px;
  border-bottom: 1px solid rgba(0, 0, 0, 0.06);
}

.drawer-menu {
  padding-top: 16px;
}

.global-footer {
  position: relative;
  width: 100%;
  z-index: 5;
  margin-top: auto;
}

.mobile-header {
  display: none;
}

/* 动画效果 */
@keyframes pulse {
  0% { transform: scale(1); }
  50% { transform: scale(1.1); }
  100% { transform: scale(1); }
}

/* 响应式设计 */
@media (max-width: 768px) {
  .sidebar {
    display: none;
  }

  .site-layout {
    margin-left: 0; /* 移动端不需要左边距 */
  }

  .mobile-header {
    display: block;
    z-index: 9;
  }

  .header-toolbar {
    display: none;
  }

  .content {
    margin: 16px;
    padding: 16px;
  }

  .main-layout {
    flex-direction: column;
  }

  .main-content-wrapper {
    min-height: calc(100vh - 110px); /* 移动端调整最小高度 */
  }
}
</style>
